<gm:page title="CRUD Application" authenticate="true">

  <!-- 
       The crud application demonstrates the using the JS API to perform CRUD 
       (Create, Retrieve, Update, Delete) operations on a feed in the datastore
       @author: GME Team
  -->
  <h1> JavaScript CRUD </h1>
  <p> Click one of the buttons below to perform an operation using the Google
      Mashup Editor's JavaScript API. </p>

  <gm:list id="data" data="${app}/crud"/>

  <input type="button" value="Create" onclick="create()"/>
  <input type="button" value="Read" onclick="read()"/>
  <input type="button" value="Update" onclick="update()"/>
  <input type="button" value="Delete" onclick="del()"/>
 
  <script>

    var gpathTitle = new GPath("atom:title");
    
    function create() {
      var d = google.mashups.getObjectById('data').getData();
      var e = d.createEntry();
      gpathTitle.setValue(e, prompt('Title:', ''));
      d.addEntry(e);
    };

    function read() {
      var e = google.mashups.getObjectById('data').getSelectedEntry();
      if (!e) { alert('Select an item'); return; }
      var d = google.mashups.getObjectById('data').getData();
      alert(gpathTitle.getValue(e));
    };

    function update() {
      var e = google.mashups.getObjectById('data').getSelectedEntry();
      if (!e) { alert('Select an item'); return; }
      var d = google.mashups.getObjectById('data').getData();
      gpathTitle.setValue(e, prompt('New title:', gpathTitle.getValue(e)));
      d.updateEntry(e);
    };

    function del() {
      var e = google.mashups.getObjectById('data').getSelectedEntry();
      if (!e) { alert('Select an item'); return; }
      var d = google.mashups.getObjectById('data').getData();
      d.removeEntry(e);
    };
  </script>

</gm:page>

